package com.jivesoftware.spark
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Panel;
	import mx.core.SpriteAsset;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	public class DragPanel extends Panel
	{
		public function DragPanel()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
		[Bindable]
		public var myTitleBar:UIComponent;
		
		private function creationCompleteHandler(event:Event):void
		{
			myTitleBar = titleBar;
			addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
		}
		
		protected var minShape:SpriteAsset;
		protected var restoreShape:SpriteAsset;
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			minShape = new SpriteAsset();
			minShape.addEventListener(MouseEvent.MOUSE_DOWN, minPanelSizeHandler);
			
			restoreShape = new SpriteAsset();
			restoreShape.addEventListener(MouseEvent.MOUSE_DOWN, restorePanelSizeHandler);
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			minShape.graphics.clear();
			minShape.graphics.lineStyle(0, 0, 0);
			minShape.graphics.beginFill(0xFFFFFF, 0.0);
			minShape.graphics.drawRect(unscaledWidth - 35, 12, 8, 8);
			
			minShape.graphics.lineStyle(2);
			minShape.graphics.beginFill(0xFFFFFF, 0.0);
			minShape.graphics.drawRect(unscaledWidth - 35, 18, 8, 2);
			
			restoreShape.graphics.clear();
			restoreShape.graphics.lineStyle(2);
			restoreShape.graphics.beginFill(0xFFFFFF, 0.0);
			restoreShape.graphics.drawRect(unscaledWidth - 20, 12, 8, 8);
			restoreShape.graphics.moveTo(unscaledWidth - 20, 15);
			restoreShape.graphics.lineTo(unscaledWidth - 12, 15);
			
			graphics.clear();
			if (isMinimized == false)
			{
				graphics.lineStyle(2);
				graphics.moveTo(unscaledWidth - 6, unscaledHeight - 1);
				graphics.curveTo(unscaledWidth - 6, unscaledHeight - 3, unscaledWidth - 1, unscaledHeight - 6);
				graphics.moveTo(unscaledWidth - 6, unscaledHeight - 4);
				graphics.curveTo(unscaledWidth - 5, unscaledHeight - 5, unscaledWidth - 4, unscaledHeight - 6);
			}
		}
		
		private var myRestoreHeight:int;
		private var isMinimized:Boolean = false;
		
		private function minPanelSizeHandler(event:Event):void
		{
			if (isMinimized != true)
			{
				myRestoreHeight = height;
				height = titleBar.height;
				isMinimized = true;
				removeEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
			}
		}
		
		private function restorePanelSizeHandler(event:Event):void
		{
			if (isMinimized == true)
			{
				height = myRestoreHeight;
				isMinimized = false;
				addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
			}
		}
		
		public static const RESIZE_CLICK:String = "resizeClick";
		
		public function resizeHandler(event:MouseEvent):void
		{
			var lowerLeftX:Number = x + width;
			var lowerRightX:Number = y + height;
			
			var upperLeftX:Number = lowerLeftX - 7;
			var upperLeftY:Number = lowerLeftY - 7;
			
			var panelRelX:Number = event.localX + x;
			var panelRelY:Number = event.localY + y;
			
			if (upperLeftX <= panelRelX && panelRelX <= lowerLeftX)
			{
				if (upperLeftY <= panelRelY && panelRelY <= lowerLeftY)
				{
					event.stopPropagation();
					var rbEvent:MouseEvent = new MouseEvent(RESIZE_CLICK);
					rbEvent.localX = event.stageX;
					rbEvent.localY = event.stageY;
					dispatchEvent(rbEvent);
				}
			}
		}
	}
}
